import {IGoodsPageRow} from "@/api/modules/pointsMall";
import {Ref} from "vue";
import {IUpdateGiftPackData} from "@/api/modules/vipCenter";

export function useEditGiftPack () {

  const formatDataForServer = (form: any) => {
    const _form = <IUpdateGiftPackData>JSON.parse(JSON.stringify(form))

    if (_form.packNum === -1) {
      _form.products.forEach(item => item.addOrDeductionStockCount = _form.addOrDeductionStockCount)
    }

    return _form
  }

  const formatDataForPage = (form: any) => {
    const _form = JSON.parse(JSON.stringify(form))

    _form.addOrDeductionStockCount = _form.addOrDeductionStockCount ?? 0

    return _form
  }

  const isShowEditGiftPack = ref(false)
  const currentGiftPackEditingRow:Ref<IGoodsPageRow | undefined> = ref()
  const showEditGiftPack = (rowData: IGoodsPageRow) => {
    isShowEditGiftPack.value = true
    currentGiftPackEditingRow.value = rowData
  }

  return {
    formatDataForPage,
    formatDataForServer,
    isShowEditGiftPack,
    currentGiftPackEditingRow,
    showEditGiftPack,
  }
}
